package avicit.bdp.dlm.rest;

import avicit.bdp.common.service.dto.CommonTypeDTO;
import avicit.bdp.common.service.dto.DataSourceDTO;
import avicit.bdp.dlm.service.DlmDataSourceService;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 存储接入REST接口
 */
@RestController
@Api(tags = "存储接入REST接口")
@RequestMapping("/api/bdp/dlm/dataSource")
public class DlmDatasourceRest {

    @Autowired
    private DlmDataSourceService dlmDataSourceService;

    /**
     * 新增存储接入
     */
    @ApiOperation(value = "新增对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeId", value = "分类id", dataType = "String", paramType = "body", required = true),
            @ApiImplicitParam(name = "dataSourceIdList", value = "数据源id集合，逗号分隔", dataType = "String", paramType = "body", required = true)
    })
    @PostMapping("/addLakeDataSource")
    public ResponseMsg<String> addLakeDataSource(@ApiIgnore @RequestBody Map<String, String> queryMap) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        dlmDataSourceService.addLakeDataSource(queryMap);
        return responseMsg;
    }

    /**
     * 设置总容量
     */
    @ApiOperation(value = "修改可用容量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataSourceId", value = "数据源id", dataType = "String", paramType = "body", required = true),
            @ApiImplicitParam(name = "availableAmount", value = "设置的总容量", dataType = "Long", paramType = "body", required = true)
    })
    @PostMapping("/updateAvailableAmount")
    public ResponseMsg<String> updateAvailableAmount(@ApiIgnore @RequestBody Map<String, String> queryMap) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        dlmDataSourceService.updateAvailableAmount(queryMap);
        return responseMsg;
    }

    /**
     * 移除关联数据源
     */
    @ApiOperation(value = "移除关联数据源")
    @ApiImplicitParam(name = "dataSourceId", value = "数据源id", dataType = "String", paramType = "body", required = true)
    @PostMapping("/delete")
    public ResponseMsg<Integer> deleteDlmDataSource(@ApiIgnore @RequestBody Map<String, String> queryMap) {

        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        dlmDataSourceService.deleteDlmDataSource(queryMap);
        return responseMsg;
    }

    /**
     * 分页查询关联数据源
     */
    @ApiOperation(value = "按条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类Id", name = "typeId", dataType = "String"),
            @ApiImplicitParam(value = "查询关键字", name = "keyWords", dataType = "String")
    })
    @GetMapping("/list")
    public ResponseMsg<Map<String, List<DataSourceDTO>>> list(
            @RequestParam String typeId,
            @RequestParam(required = false) String keywords) {

        ResponseMsg<Map<String, List<DataSourceDTO>>> responseMsg = new ResponseMsg<>();
        Map<String, List<DataSourceDTO>> result =
                dlmDataSourceService.searchDlmDataSource(typeId, keywords);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    /**
     * 分页查询关联数据源
     */
    @ApiOperation(value = "分页查询关联数据源")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类Id", name = "typeId", dataType = "String"),
            @ApiImplicitParam(value = "查询关键字", name = "keyWords", dataType = "String")
    })
    @GetMapping("/dataSourceList")
    public ResponseMsg<Map<String, List<DataSourceDTO>>> dataSourceList(
            @RequestParam String typeId,
            @RequestParam(required = false) String keywords) {

        ResponseMsg<Map<String, List<DataSourceDTO>>> responseMsg = new ResponseMsg<>();
        Map<String, List<DataSourceDTO>> result =
                dlmDataSourceService.searchDataSource(typeId, keywords);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }


    /**
     * 查询接入通用树结构（同步）
     */
    @ApiOperation(value = "查询接入通用树结构")
    @GetMapping("/findStorageCommonType")
    public ResponseMsg<List<CommonTypeDTO>> findStorageCommonType() {
        ResponseMsg<List<CommonTypeDTO>> responseMsg = new ResponseMsg<>();
        List<CommonTypeDTO> result = dlmDataSourceService.findStorageCommonType();
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

}
